package top.jle.wx.proxy.facade.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import top.jle.jpa.basic.exception.BasicCustomException;
import top.jle.jpa.basic.util.ExceptionUtils;
import top.jle.wx.proxy.domain.fans.FansInfo;
import top.jle.wx.proxy.exception.ExceptionCode;
import top.jle.wx.proxy.facade.FansFacade;
import top.jle.wx.proxy.feign.BasicAPIFeign;
import top.jle.wx.proxy.feign.ResultCheckUtil;
import top.jle.wx.proxy.feign.fans.FansAPIFeign;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import top.jle.wx.proxy.constant.FansOption;

import java.util.List;

@Component("fansFacade")
public class FansFacadeImpl implements FansFacade {

    @Autowired
    private FansAPIFeign fansAPIFeign;

    private static final Logger LOGGER = LoggerFactory.getLogger(FansFacadeImpl.class);

    @Override
    public FansInfo getFansInfo(String openId){
        if(StringUtils.isBlank(openId)){
            ExceptionUtils.throwInvalidParamException();
        }

        JSONObject result = fansAPIFeign.fansInfo(openId);
        if(!ResultCheckUtil.check(result)){
            throw new BasicCustomException(ExceptionCode.GET_FANS_INFO_FAIL);
        }
        return result.toJavaObject(FansInfo.class);
    }

    @Override
    public List<FansInfo> batchGetFansInfo( List<String> openIdList){
        if(CollectionUtils.isEmpty(openIdList) || openIdList.size()> FansOption.FANS_INFO_LIST_LIMIT){
            ExceptionUtils.throwInvalidParamException();
        }

        JSONArray openIds = new JSONArray();
        for(String openId:openIdList){
            JSONObject item = new JSONObject();
            item.put("openid",openId);
            openIds.add(item);
        }
        JSONObject options = new JSONObject();
        options.put("user_list",openIds);
        JSONObject result = fansAPIFeign.fansInfoList(options);
        if(!ResultCheckUtil.check(result)){
            throw new BasicCustomException(ExceptionCode.GET_FANS_INFO_LIST_FAIL);
        }
        JSONArray list = result.getJSONArray("user_info_list");
        return list.toJavaList(FansInfo.class);

    }

    @Override
    public FansOption.FansIdListResponse getFansIds(String nextOpenId){

        JSONObject result = fansAPIFeign.getFansIdList(nextOpenId);
        LOGGER.info(result.toJSONString());
        if(!ResultCheckUtil.check(result)){
            throw new BasicCustomException(ExceptionCode.GET_FANS_ID_LIST_FAIL);
        }
        FansOption.FansIdListResponse response = new FansOption.FansIdListResponse();
        response.setTotal(result.getInteger("total"));
        response.setCount(result.getInteger("count"));
        if(response.getCount()< FansOption.FANS_ID_LIST_LIMIT || (response.getCount()== FansOption.FANS_ID_LIST_LIMIT && response.getTotal()== FansOption.FANS_ID_LIST_LIMIT)){
            response.setNextOpenId("");
        }else{
            response.setNextOpenId(result.getString("next_openid"));
        }
        if(result.containsKey("data")){
            response.setOpenIds(result.getJSONObject("data").getJSONArray("openid").toJavaList(String.class));
        }

        return response;
    }



}
